import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, LabelEncoder, OneHotEncoder
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

# 加载数据
data = pd.read_csv(r"C:\Users\卷\Desktop\程序\Pycharm\24.3.25\dataset.csv")

# 分离数值型和非数值型特征
numeric_data = data.select_dtypes(include=['int', 'float'])
categorical_data = data.select_dtypes(include=['object'])

# 填充缺失值
numeric_imputer = SimpleImputer(strategy='mean')
numeric_data_filled = pd.DataFrame(numeric_imputer.fit_transform(numeric_data), columns=numeric_data.columns)

categorical_imputer = SimpleImputer(strategy='most_frequent')
categorical_data_filled = pd.DataFrame(categorical_imputer.fit_transform(categorical_data), columns=categorical_data.columns)

# 对非数值型数据进行独热编码
onehot_encoder = OneHotEncoder()
categorical_data_encoded = pd.DataFrame(onehot_encoder.fit_transform(categorical_data_filled).toarray())

# 合并处理后的数据
data_filled = pd.concat([numeric_data_filled, categorical_data_encoded], axis=1)

# 将列名转换为字符串类型
data_filled.columns = data_filled.columns.astype(str)

# 进行标准化
scaler = StandardScaler()
data_filled = pd.DataFrame(scaler.fit_transform(data_filled), columns=data_filled.columns)

# 构建多维模型结构
kmeans = KMeans(n_clusters=3, random_state=0)
data_filled['cluster'] = kmeans.fit_predict(data_filled)

# 数据统计分析挖掘
cluster_counts = data_filled['cluster'].value_counts()
print("每个簇中的样本数量:")
print(cluster_counts)

# 可视化簇的分布
plt.figure(figsize=(8, 6))
plt.scatter(data_filled['age'], data_filled['call_duration'], c=data_filled['cluster'], cmap='viridis')
plt.xlabel('Age')
plt.ylabel('Call Duration')
plt.title('Cluster Analysis')
plt.colorbar(label='Cluster')
plt.show()

from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, classification_report

# 划分特征和标签
X = data_filled.drop('cluster', axis=1)
y = data_filled['cluster']

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


# 定义函数来训练决策树模型并评估性能
def train_decision_tree(X_train, y_train, X_test, y_test):
    # 初始化决策树分类器
    dt_classifier = DecisionTreeClassifier(random_state=42)

    # 训练模型
    dt_classifier.fit(X_train, y_train)

    # 在测试集上进行预测
    y_pred = dt_classifier.predict(X_test)

    # 计算准确率
    accuracy = accuracy_score(y_test, y_pred)

    # 打印分类报告
    report = classification_report(y_test, y_pred)

    return accuracy, report


# 训练决策树模型并评估性能
dt_accuracy, dt_report = train_decision_tree(X_train, y_train, X_test, y_test)

print("决策树模型准确率:", dt_accuracy)
print("决策树模型分类报告:")
print(dt_report)

from sklearn.naive_bayes import GaussianNB

# 初始化朴素贝叶斯分类器
nb_classifier = GaussianNB()

# 训练模型
nb_classifier.fit(X_train, y_train)

# 在测试集上进行预测
y_pred_nb = nb_classifier.predict(X_test)

# 计算准确率
accuracy_nb = accuracy_score(y_test, y_pred_nb)

# 打印分类报告
report_nb = classification_report(y_test, y_pred_nb)

print("朴素贝叶斯模型准确率:", accuracy_nb)
print("朴素贝叶斯模型分类报告:")
print(report_nb)

from sklearn.neural_network import MLPClassifier

# 初始化多层感知器分类器
mlp_classifier = MLPClassifier(hidden_layer_sizes=(100, 50), max_iter=1000)

# 训练模型
mlp_classifier.fit(X_train, y_train)

# 在测试集上进行预测
y_pred_mlp = mlp_classifier.predict(X_test)

# 计算准确率
accuracy_mlp = accuracy_score(y_test, y_pred_mlp)

# 打印分类报告
report_mlp = classification_report(y_test, y_pred_mlp)

print("多层感知器模型准确率:", accuracy_mlp)
print("多层感知器模型分类报告:")
print(report_mlp)

from sklearn.svm import SVC

# 初始化支持向量机分类器
svm_classifier = SVC(kernel='linear')

# 训练模型
svm_classifier.fit(X_train, y_train)

# 在测试集上进行预测
y_pred_svm = svm_classifier.predict(X_test)

# 计算准确率
accuracy_svm = accuracy_score(y_test, y_pred_svm)

# 打印分类报告
report_svm = classification_report(y_test, y_pred_svm)

print("支持向量机模型准确率:", accuracy_svm)
print("支持向量机模型分类报告:")
print(report_svm)